package com.mew.slots.core.game.jdqe.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.dwsl.bo.DwslInRoomList;
import com.mew.slots.core.game.jdqe.bo.JdqeBonusType;
import com.mew.slots.core.game.jdqe.bo.JdqeInRoom;
import com.mew.slots.core.game.jdqe.message.JdqeBonusPlayResponseMessage;
import com.mew.slots.core.game.jdqe.message.JdqeJackpotBonusPlayResponseMessage;
import com.mew.slots.core.game.mjk.bo.MjkBonusType;
import com.mew.slots.core.game.mjk.bo.MjkInRoom;
import com.mew.slots.core.game.mjk.message.MjkBonusHelpEggMessage;
import com.mew.slots.core.game.mjk.message.MjkBonusOpenHatMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class JdqeBonusService {
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message=new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsType() != JdqeBonusType.parse(player.getBonusType()).getBonusType()  || winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }

        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        JdqeInRoom in = JSON.parseObject(winFree.getJson(),JdqeInRoom.class);
        if(player.getBonusType() == JdqeBonusType.Bonus.code()){
            message = bonus(player,in);
        }else if(player.getBonusType()== JdqeBonusType.JP.code()){
            if(player.getOtherMsg().length != 1 || in.getJp()[player.getOtherMsg()[0]] > 0){
                player.setStat(ResponseCode.BounsParamInvalid.code());
                return message;
            }
            message = jp(player,in);
        }else{
            player.setStat(ResponseCode.WinFreeValidError.code());
            return message;
        }
        winFree.setJson(JSON.toJSONString(in));
        return message;
    }

    /**
     * 普通特殊
     * @param player
     * @return
     */
    private JdqeBonusPlayResponseMessage bonus(StrategyGameVO player,JdqeInRoom in){
        JdqeBonusPlayResponseMessage message=new JdqeBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        int icon = RandomUtil.randomInt(9);
        message.setIcon(icon);
        if(in.getBonus()[icon] == 0){
            in.getBonus()[icon] = 1;
            winFree.setBounsRate(winFree.getBounsRate()+5);
            //判断四个区域是否全满

            //左边两列区域
            if(icon%3 < 2){
                //左上
                if(icon/3 < 2){
                    if(in.getBonus()[0] == 1&&in.getBonus()[1] == 1&&in.getBonus()[3] == 1&&in.getBonus()[4] ==1){
                        winFree.setBounsRate(winFree.getBounsRate()+10);
                        message.getBrs()[0] = 1;
                    }
                }
                //左下
                if(icon/3 > 0){
                    if(in.getBonus()[3] == 1&&in.getBonus()[4] == 1&&in.getBonus()[6] == 1&&in.getBonus()[7] ==1){
                        winFree.setBounsRate(winFree.getBounsRate()+10);
                        message.getBrs()[2] = 1;
                    }
                }
            }
            //右边两列区域
            if(icon%3 > 0){
                //右上
                if(icon/3 < 2){
                    if(in.getBonus()[1] == 1&&in.getBonus()[2] == 1&&in.getBonus()[4] == 1&&in.getBonus()[5] ==1){
                        winFree.setBounsRate(winFree.getBounsRate()+10);
                        message.getBrs()[2] = 1;
                    }
                }
                //右下
                if(icon/3 > 0){
                    if(in.getBonus()[4] == 1&&in.getBonus()[5] == 1&&in.getBonus()[7] == 1&&in.getBonus()[8] ==1){
                        winFree.setBounsRate(winFree.getBounsRate()+10);
                        message.getBrs()[3] = 1;
                    }
                }
            }
        }
        winFree.setBounsNumber(winFree.getBounsNumber()-1);
        message.setBonusNum(winFree.getBounsNumber());
        message.setAllWin(winFree.getBounsRate()*winFree.getBet()*player.getLineNumber());
        if(winFree.getBounsNumber() <= 0){
            message.setIsGameOver(1);
            winFree.setBounsType(0);
            winFree.setBounsRate(0);
            player.setPaymentNumber(message.getAllWin());
        }
        player.setIconResult(String.valueOf(icon));
        return message;
    }

    /**
     * 奖池特殊
     * @param player
     * @param in
     * @return
     */
    private JdqeJackpotBonusPlayResponseMessage jp(StrategyGameVO player,JdqeInRoom in){
        int open = player.getOtherMsg()[0];
       JdqeJackpotBonusPlayResponseMessage message = new JdqeJackpotBonusPlayResponseMessage();
       int type = RandomUtil.randomInt(1,5);
       in.getJp()[open] = type;
       message.setIcon(type);
       long num = Arrays.stream(in.getJp()).filter(i -> i == message.getIcon()).count();
        if(num > 2){
            message.setIsGameOver(1);
            message.setJpNo(type);
            player.setWinType(SlotsWinType.Jackpot.code());
            player.getWinFree().setBounsType(0);
            player.getWinFree().setBounsNumber(0);
        }
        player.setIconResult(String.valueOf(type));
       return message;
    }

    public static void main(String[] args) {
        StrategyGameVO player = SlotsGameUtil.buildTestStrategyGameVO();
        player.setBetNumber(1l);
        player.setLineNumber(50);
        WinFree winFree = player.getWinFree();
        winFree.setBet(1l);
        winFree.setLastBetNumber(1l);
        JdqeBonusPlayResponseMessage res;
        long  allRate = 0;
        for (int i = 0; i < 1000000; i++) {
            winFree.setBounsType(1);
            winFree.setBounsNumber(10);
            JdqeInRoom in = new JdqeInRoom();
            do{
                res = new JdqeBonusService().bonus(player, in);
            }while(res.getIsGameOver() == 0);
            allRate += (res.getAllWin()/winFree.getBet()/player.getLineNumber());
        }
        System.out.println(allRate/1000000);
    }
}
